/* This dofile is to "re-run" the randomization 
and calculate the potential share of mentees of each school 
for both grade 10 and grade 12 students 

for details,
see dofiles 1.1.1 and 1.2.1 for grade 10 
see dofiles 1.1.2 and 1.2.2 for grade 12 

Idea: identify the number of available mentors in each strata (fixed for all randomizations)

In each randomization trial, obtain the number of treated schools, based on which we compute the potential share such that the number of mentors is proportional to grade size 

After 1000 trials,  calculate the average potential share for each school 

Save these information, to be merged to the output from step dofiles 4.1.cleaning_gradeXX
*/

version 17
clear all
capture log close
set varabbrev off
set more off

* generate value of random seeds 
drop _all
set obs 1000
set seed 142857 
g value=int(10^6*runiform())
qui levelsof value
gl randomseed "`r(levels)'"

********************************************************************************
*
*					GRADE 10
*
********************************************************************************
clear 
g schoolname_bl=""
g sharementee=.
save $temp/sharementee, replace emptyok

use "$temp/stem_analysis.dta", clear
drop schoolname_b
drop if missing(schoolname_bl)

* number of available mentors 
scalar nmentor_stem_cst = 39
scalar nmentor_stem_jnec = 36
scalar nmentor_stem_gcit = 44
scalar nmentor_stem_shrubtse = 48
scalar nmentor_stem_cnr = 23
scalar nmentor_stem_rtc = 10

bys schoolname_bl: egen totstudent = total(~missing(uniqueid))

duplicates drop schoolname_bl, force 

* stratificatioon 
g group=1 if stem_cst_bl==1
replace group=2 if stem_jnec_bl==1
replace group=3 if stem_gcit_bl==1
replace group=4 if stem_shrubtse_bl==1
replace group=5 if stem_cnr_bl==1
replace group=6 if stem_rtc_bl==1

* check if it is feasible to randomize with 
egen geopri = group(group private_bl)
tab geopri

levelsof geopri 
global geoprilist "`r(levels)'"
g groupsize=.
foreach i of global geoprilist {
	summ total910_bl if geopri==`i', de
	local med`i' = r(p50)
	dis "`med`i''"
	replace groupsize = 1 if total910_bl<=`med`i'' & geopri==`i'
	replace groupsize = 2 if total910_bl>`med`i''  & geopri==`i'
}

egen geoprisize = group(group private_bl groupsize)
tab geoprisize
replace geoprisize = 13 if geoprisize==14

* randomization, stratified by geographic + private group + size
foreach j of global randomseed {
cap drop treat*
bys geoprisize: g rand_num`j'=runiform()
bys geoprisize: egen ordering`j'=rank(rand_num`j')
bys geoprisize: g treat_`j' = ordering`j'>((_N*3-4)/4)
drop rand_num`j' ordering`j'

cap drop sharementee = .
cap g sharementee = 0

foreach i in stem_cst stem_jnec stem_gcit stem_shrubtse stem_cnr stem_rtc {
	* total students in each "strata"
	egen totalgrade10_`i' = total(totstudent*(treat_`j'==1)*(`i'_bl==1)) 
	
	* number of mentees depends on number of available mentors & school size
	replace sharementee = 5*(int(`=scalar(nmentor_`i')'*(totstudent/totalgrade10_`i')))/totstudent if `i'_bl==1 & treat_`j'==1 
	
	cap drop totalgrade10_`i'
	preserve
	keep schoolname_bl sharementee
	tempfile share`j' 
	save `share`j'', replace 
	restore 
}


preserve 
clear 
use $temp/sharementee
append using `share`j''
save $temp/sharementee, replace 
restore 
}

use $temp/sharementee, clear
bys schoolname_bl: egen potentialshare = mean(sharementee)
duplicates drop schoolname_bl, force
drop sharementee
save "$clean/sharementee_grade10.dta", replace




